﻿using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns.Controls.Renderers.Windows
{
    public abstract class ControlRenderer<TControl, TRenderer> : IControlRenderer<TControl, TRenderer> 
        where TControl : Control<TControl, TRenderer>
        where TRenderer : class, IControlRenderer<TControl, TRenderer>
    {
        #region Methods

        public void RenderUiXmlBegin(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlBegin(control, writer);
            }
        }

        public void RenderUiXml(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlBegin(control, writer);
                OnRenderUiXmlContent(control, writer);
                OnRenderUiXmlEnd(control, writer);
            }
        }

        public void RenderUiXmlEnd(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlEnd(control, writer);
            }
        }

        protected virtual void OnRenderUiXmlContent(TControl control, CodeWriter writer)
        {
        }

        protected virtual void OnRenderUiXmlBegin(TControl control, CodeWriter writer)
        {
        }

        protected virtual void OnRenderUiXmlEnd(TControl control, CodeWriter writer)
        {
        }

        public void RenderUiCode(TControl control, CodeWriter writer)
        {
            OnRenderViewModelCs(control, writer);
        }

        protected virtual void OnRenderViewModelCs(TControl control, CodeWriter writer)
        {
        }

        protected virtual string GetRenderedUid(TControl control)
        {
            return control.Uid;
        }

        protected void RenderControlAttributes(TControl control, CodeWriter writer)
        {
            writer.WriteAttribute("Style", control.Style);
            writer.WriteAttribute("x:Uid", GetRenderedUid(control));
            //writer.WriteAttribute("Controls:HelpText.Text", control.HelpText);

            writer.WriteAttribute("Height", control.Height);
            writer.WriteAttribute("Width", control.Width);

            writer.WriteAttribute("HorizontalAlignment", control.HorizontalAlignment, HorizontalAlignment.Stretch);
            writer.WriteAttribute("VerticalAlignment", control.VerticalAlignment, VerticalAlignment.Stretch);

            writer.WriteAttribute("Grid.Row", control.GridRow);
            writer.WriteAttribute("Grid.RowSpan", control.GridRowSpan);

            writer.WriteAttribute("Grid.Column", control.GridColumn);
            writer.WriteAttribute("Grid.ColumnSpan", control.GridColumnSpan);

            //if (!string.IsNullOrWhiteSpace(control.IsEnabledProperty))
            //{
            //    writer.WriteAttribute("IsEnabled", string.Format("{{Binding {0}}}", control.IsEnabledProperty));
            //}
            //if (!string.IsNullOrWhiteSpace(control.IsVisibleProperty))
            //{
            //    writer.WriteAttribute("Visibility", string.Format("{{Binding {0}, Converter={{StaticResource booleanToVisibility}}}}", control.IsVisibleProperty));
            //}
        }

        #endregion Methods
    }
}